﻿//using MySql.Data.MySqlClient;
//using Mysqlx.Crud;
using Hotel_Project.Bean;
using Hotel_Project.HotelForm;
using MySql.Data.MySqlClient;
using Org.BouncyCastle.Pqc.Crypto.Frodo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Hotel_Project.HotelForm
{
    public partial class RoomMaintenanceFrom : Form
    {
        public RoomMaintenanceFrom()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        RoomMaintenanceFromDomain roomMain = new RoomMaintenanceFromDomain();
        private void RoomMaintenanceFrom_Load(object sender, EventArgs e)
        {
            //初始化数据库资源
            using (MySqlConnection conn = new MySqlConnection(MySqlString.MYSQL_CONNAND))
            {
                conn.Open();
                using (MySqlCommand cmd = new MySqlCommand("select Data_maintenance_Id from room_data_maintenance ", conn))
                {
                    using (MySqlDataAdapter da = new MySqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        da.Fill(dt);

                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            if (roomMain.Id != Convert.ToInt32(dt.Rows[i][0].ToString()))
                            {
                                roomMain.Id = Convert.ToInt32(dt.Rows[i][0].ToString());
                            }
                            
                        }
                    }
                }
            }
        }

        private void Copy_Click(object sender, EventArgs e)
        {   
                Clipboard.SetText(StaffId_Text.Text);   
        }
        /// <summary>
        /// 连接数据库的数据
        /// </summary>
        static string name = "server=192.168.0.56;user=xxxx;database=hotel;password=123456";
        /// <summary>
        /// 数据库的数据的增加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Add_Click(object sender, EventArgs e)
        {
            using (MySqlConnection conn = new MySqlConnection(MySqlString.MYSQL_CONNAND))
            {
                conn.Open();
                using (MySqlCommand cmd = new MySqlCommand("insert into " +
                "room_data_maintenance(Data_maintenance_Id,Level,room_type,Building,Prices,Number_of_floors,Houses_facing,guest_room,Telephone_extension,Telephone,room_describe,remark) " +
                "values(@Data_maintenance_Id, @Level,@room_type,@Building,@Prices,@Number_of_floors,@Houses_facing,@guest_room,@Telephone_extension,@Telephone,@room_describe,@remark)", conn))
                {
                    try
                    {
                        if (StaffId_Text.Text != Convert.ToString(roomMain.Id))
                        {
                            //房间号
                            cmd.Parameters.AddWithValue("@Data_maintenance_Id", StaffId_Text.Text);
                            //楼层号
                            cmd.Parameters.AddWithValue("@Level", Floor_Text.Text);
                            //房间类型
                            cmd.Parameters.AddWithValue("@room_type", Room_Text.Text);
                            //楼栋号
                            cmd.Parameters.AddWithValue("@Building", Building_Text.Text);
                            //散客价格
                            cmd.Parameters.AddWithValue("@Prices", numericUpDown1.Text);
                            //房间床数
                            cmd.Parameters.AddWithValue("@Number_of_floors", numericUpDown2.Text);
                            //房屋朝向
                            cmd.Parameters.AddWithValue("@Houses_facing", Toward_Text.Text);
                            //是否为客房
                            if (GuestRoom.Checked == true)
                            {
                                //选中为客房就为1
                                cmd.Parameters.AddWithValue("@guest_room", 1);
                            }
                            else
                            {
                                cmd.Parameters.AddWithValue("@guest_room", 0);
                            }
                            //电话分机
                            cmd.Parameters.AddWithValue("@Telephone_extension", phone_Text.Text);
                            //电话外机
                            cmd.Parameters.AddWithValue("@Telephone", ExternalMachine_Text.Text);
                            //房间描述
                            cmd.Parameters.AddWithValue("@room_describe", Describe_Text.Text);
                            //备注说明
                            cmd.Parameters.AddWithValue("@remark", Notes_Text.Text);
                            cmd.ExecuteNonQuery();
                            roomMain.Id = Convert.ToInt32(StaffId_Text.Text);
                            MessageBox.Show("增加成功");
                        }
                        else MessageBox.Show("房间号重复");
                    }
                    catch (Exception) {  }
                }
            }


        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 删除代码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void delete_Click(object sender, EventArgs e)
        {
            using (MySqlConnection conn = new MySqlConnection(MySqlString.MYSQL_CONNAND))
            {
                conn.Open();
                using (MySqlCommand cmd = new MySqlCommand("delete  from room_data_maintenance  where Data_maintenance_Id = @Data_maintenance_Id", conn))
                {
                        if (StaffId_Text.Text == Convert.ToString(roomMain.Id))
                        {
                            //房间号
                            cmd.Parameters.AddWithValue("@Data_maintenance_Id", StaffId_Text.Text);
                            cmd.ExecuteNonQuery();
                            MessageBox.Show("删除成功");
                      
                             roomMain.Id = 0;

                        }
                        else MessageBox.Show("没有你要删除的房间号");
                }
            }
        }





        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void update_Click(object sender, EventArgs e)
        {
            OpenFormB();

        }
        
        /// <summary>
        /// 绑定数据表
        /// </summary>
        private void OpenFormB()
        {
            DataTable data = GetDataFromDatabase();
            updateRoomMaintenanceFrom formB = new updateRoomMaintenanceFrom();
            formB.SetDataGridViewDataSource(data, StaffId_Text.Text);
            formB.Show();
        }
        private DataTable GetDataFromDatabase()
        {
            DataTable dt = new DataTable();
            using (MySqlConnection conn = new MySqlConnection(MySqlString.MYSQL_CONNAND))
            {
                conn.Open ();
                string query = "SELECT * FROM room_data_maintenance";
                using (MySqlCommand cmd = new MySqlCommand(query, conn))
                {
                    MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                    da.Fill(dt);
                }
            }
            return dt;
        }
        /// <summary>
        /// 返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void select_Click(object sender, EventArgs e)
        {
            this.Hide();//隐藏当前窗口页面
            //下次启动在show就行了
        }

      
    }
}
